using System;
using System.Collections;
//-----------------------------------------------------------------------------
namespace MonoBED3
{
//-----------------------------------------------------------------------------	
	public class World
	{
		private ArrayList sectors;
		private ArrayList actors;
		
		public br.odb.libscene.World makeBZK3World() {
			
			br.odb.libscene.World world = new br.odb.libscene.World();
			br.odb.libscene.Sector sector;
			
			for ( int c = 0; c < this.getTotalSectors(); ++c ) {
				
				sector = getSector( c ).makeBZK3Sector();
				world.addSector( sector );				
			}			
			
			return world;
		}
		
		public void copyFrom( World other ) {
			sectors = new ArrayList( other.sectors.Count );
			actors = new ArrayList( other.actors.Count );
			
			foreach ( Sector s in other.sectors ) {
				sectors.Add( new Sector( s ) );				
			}
			
			foreach ( Actor a in other.actors ) {
				actors.Add( new Actor( a ) );				
			}			
		}
//-----------------------------------------------------------------------------		
		public World ()
		{
			sectors = new ArrayList();
			Sector sector = new Sector();
			sector.setIsParent( true );
			sectors.Add( sector );
			actors = new ArrayList();
		}
//-----------------------------------------------------------------------------
		public void addSector( Sector s )
		{
			s.setId( sectors.Count );
			sectors.Add( s );			
		}		
//-----------------------------------------------------------------------------
		public Sector addNewSector()
		{
			Sector sector = new Sector();
			sector.setId( sectors.Count );
			sectors.Add(sector);			
			return sector;
		}
//-----------------------------------------------------------------------------
		public Actor addNewActor()
		{
			Actor actor = new Actor();
			actor.setId( actors.Count );
			actors.Add(actor);
			return actor;
		}		
//-----------------------------------------------------------------------------
		public Sector getSector(int index)
		{
			Sector sector = null;
			if (index  < sectors.Count)
				sector = (Sector)sectors[index];
			return sector;
		}
//-----------------------------------------------------------------------------
		public Actor getActor(int index)
		{
			Actor actor = null;
			if (index  < actors.Count)			
				actor = (Actor)actors[index];
			return actor;
		}	
		
		public int getTotalSectors() {
			return sectors.Count;
		}
		
		public void eraseAll() {

			sectors.RemoveRange( 0, sectors.Count );
		}
		
		public int getTotalActors() {
			return actors.Count;
		}
		
		public int getSectorAt( float x, float y, float z ) {

			for ( int c = 1;  c < sectors.Count; ++c ) {

				if ( ( sectors[ c ] as Sector ).contains( x, y, z ) ) {

					return c;
				}
			}			
			return -1;
		}		
		
		public void removeActor( Actor a ) {
			this.actors.Remove( a );
		}
		
		public void removeSector( Sector s )  {
			this.sectors.Remove( s );
			for ( int c = 0; c < sectors.Count; ++c )
				getSector( c ).setId( c );
				      
		}
		
		public int getActorAt( float x, float y, float z ) {
			for ( int c = 0;  c < actors.Count; ++c ) {
				if ( ( actors[ c ] as Actor ).contains( x, y, z ) )
					return c;
			}
			
			return -1;
		}

//-----------------------------------------------------------------------------
		public override string ToString ()
		{
			String toReturn = "";
			
			for ( int c = 0; c < sectors.Count; ++c ) {
				toReturn += getSector( c ).ToString() + "\n";
			}
			
			for ( int c = 0; c < actors.Count; ++c ) {
				toReturn += getActor( c ).ToString() + "\n";
			}
			
			return toReturn;
		}
	}
//-----------------------------------------------------------------------------	
}
//-----------------------------------------------------------------------------